<template><div><h2 id="laravel-fortify" tabindex="-1"><a class="header-anchor" href="#laravel-fortify"><span>Laravel Fortify</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a>
<ul>
<li><a href="#what-is-fortify">Fortify 是什么？</a></li>
<li><a href="#when-should-i-use-fortify">何时使用 Fortify?</a></li>
</ul>
</li>
<li><a href="#installation">安装</a>
<ul>
<li><a href="#the-fortify-service-provider">Fortify 服务提供者</a></li>
<li><a href="#fortify-features">Fortify 功能</a></li>
<li><a href="#disabling-views">禁用视图</a></li>
</ul>
</li>
<li><a href="#authentication">认证</a>
<ul>
<li><a href="#customizing-user-authentication">自定义用户身份验证</a></li>
<li><a href="#customizing-the-authentication-pipeline">自定义身份验证渠道</a></li>
<li><a href="#customizing-authentication-redirects">自定义重定向</a></li>
</ul>
</li>
<li><a href="#two-factor-authentication">双重认证</a>
<ul>
<li><a href="#enabling-two-factor-authentication">启用双重认证</a></li>
<li><a href="#authenticating-with-two-factor-authentication">使用双重认证</a></li>
<li><a href="#disabling-two-factor-authentication">禁用双重认证</a></li>
</ul>
</li>
<li><a href="#registration">注册</a>
<ul>
<li><a href="#customizing-registration">自定义注册</a></li>
</ul>
</li>
<li><a href="#password-reset">重置密码</a>
<ul>
<li><a href="#requesting-a-password-reset-link">请求密码重置链接</a></li>
<li><a href="#resetting-the-password">重置密码</a></li>
<li><a href="#customizing-password-resets">自定义重置密码</a></li>
</ul>
</li>
<li><a href="#email-verification">邮件认证</a>
<ul>
<li><a href="#protecting-routes">保护路由</a></li>
</ul>
</li>
<li><a href="#password-confirmation">确认密码</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><a href="https://github.com/laravel/fortify" target="_blank" rel="noopener noreferrer">Laravel Fortify</a> 是一个在Laravel中与前端无关的身份认证后端实现。 Fortify 注册了所有实现 Laravel 身份验证功能所需的路由和控制器, 包括登录、注册、重置密码、邮件验证等。安装 Fortify 后，你可以运行 Artisan 命令 <code v-pre>route:list</code> 来查看 Fortify 已注册的路由。</p>
<p>由于 Fortify 不提供其自己的用户界面，因此它应该与你自己的用户界面配对，该用户界面向其注册的路由发出请求。在本文档的其余部分中，我们将进一步讨论如何向这些路由发出请求。</p>
<blockquote>
<p><strong>提示</strong><br>
请记住， Fortify 是一个软件包，旨在使你能够快速开始实现 Laravel 的身份验证功能。 <strong>你并非一定要使用它。</strong> 你始终可以按照以下说明中提供的文档，自由地与 Laravel 的身份认证服务进行交互， <a href="https://learnku.com/docs/laravel/10.x/authentication" target="_blank" rel="noopener noreferrer">用户认证</a>， <a href="https://learnku.com/docs/laravel/10.x/passwords" target="_blank" rel="noopener noreferrer">重置密码</a> 和 <a href="https://learnku.com/docs/laravel/10.x/verification" target="_blank" rel="noopener noreferrer">邮箱认证</a> 文档。</p>
</blockquote>
<h3 id="fortify-是什么" tabindex="-1"><a class="header-anchor" href="#fortify-是什么"><span>Fortify 是什么？</span></a></h3>
<p>如上所述，Laravel Fortify 是一个与前端无关的身份认证后端实现，Fortify 注册了所有实现 Laravel 身份验证功能所需的路由和控制器，包括登录，注册，重置密码，邮件认证等。</p>
<p><strong>你不必使用 Fortify，也可以使用 Laravel 的身份认证功能。</strong> 你始终可以按照 <a href="https://learnku.com/docs/laravel/10.x/authentication" target="_blank" rel="noopener noreferrer">用户认证</a>，<a href="https://learnku.com/docs/laravel/10.x/passwords" target="_blank" rel="noopener noreferrer">重置密码</a> 和 <a href="https://learnku.com/docs/laravel/10.x/verification" target="_blank" rel="noopener noreferrer">邮箱认证</a> 文档中提供的文档来手动与 Laravel 的身份验证服务进行交互。</p>
<p>如果你是一名新手，在使用 Laravel Fortify 之前不妨尝试使用 <a href="https://learnku.com/docs/laravel/10.x/starter-kits" target="_blank" rel="noopener noreferrer">Laravel Breeze</a> 应用入门套件。Laravel Breeze 为你的应用提供身份认证支架，其中包括使用 <a href="https://tailwindcss.com/" target="_blank" rel="noopener noreferrer">Tailwind CSS</a>。与 Fortify 不同，Breeze 将其路由和控制器直接发布到你的应用程序中。这使你可以学习并熟悉 Laravel 的身份认证功能，然后再允许 Laravel Fortify 为你实现这些功能。</p>
<p>Laravel Fortify 本质上是采用了 Laravel Breeze 的路由和控制器，且提供了不包含用户界面的扩展。这样，你可以快速搭建应用程序身份认证层的后端实现，而不必依赖于任何特定的前端实现。</p>
<h3 id="何时使用-fortify" tabindex="-1"><a class="header-anchor" href="#何时使用-fortify"><span>何时使用 Fortify？</span></a></h3>
<p>你可能想知道何时使用 Laravel Fortify。首先，如果你正在使用 Laravel 的 <a href="https://learnku.com/docs/laravel/10.x/starter-kits" target="_blank" rel="noopener noreferrer">应用入门套件</a>，你不需要安装 Laravel Fortify，因为它已经提供了完整的身份认证实现。</p>
<p>如果你不使用应用入门套件，并且你的应用需要身份认证功能，则有两个选择：手动实现应用的身份认证功能或使用由 Laravel Fortify 提供这些功能的后端实现。</p>
<p>如果你选择安装 Fortify，你的用户界面将向 Fortify 的身份验证路由发出请求，本文档中对此进行了详细介绍，以便对用户进行身份认证和注册。</p>
<p>如果你选择手动与 Laravel 的身份认证服务进行交互而不是使用 Fortify，可以按照 <a href="https://learnku.com/docs/laravel/10.x/authentication" target="_blank" rel="noopener noreferrer">用户认证</a>，<a href="https://learnku.com/docs/laravel/10.x/passwords" target="_blank" rel="noopener noreferrer">重置密码</a> 和 <a href="https://learnku.com/docs/laravel/10.x/verification" target="_blank" rel="noopener noreferrer">邮箱认证</a> 文档中提供的说明进行操作。</p>
<h4 id="laravel-fortify-laravel-sanctum" tabindex="-1"><a class="header-anchor" href="#laravel-fortify-laravel-sanctum"><span>Laravel Fortify &amp; Laravel Sanctum</span></a></h4>
<p>一些开发人员对 <a href="https://learnku.com/docs/laravel/10.x/sanctum" target="_blank" rel="noopener noreferrer">Laravel Sanctum</a> 和 Laravel Fortify 两者之间的区别感到困惑。由于这两个软件包解决了两个不同但相关的问题，因此 Laravel Fortify 和 Laravel Sanctum 并非互斥或竞争的软件包。</p>
<p>Laravel Sanctum 只关心管理 API 令牌和使用会话 cookie 或令牌来认证现有用户。Sanctum 不提供任何处理用户注册，重置密码等相关的路由。</p>
<p>如果你尝试为提供 API 或用作单页应用的后端的应用手动构建身份认证层，那么完全有可能同时使用 Laravel Fortify（用于用户注册，重置密码等）和 Laravel Sanctum（API 令牌管理，会话身份认证）。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>首先，使用 Composer 软件包管理器安装 Fortify：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/fortify</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>下一步，使用 <code v-pre>vendor:publish</code> 命令来发布 Fortify 的资源：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan vendor:publish <span class="token parameter variable">--provider</span><span class="token operator">=</span><span class="token string">"Laravel\Fortify\FortifyServiceProvider"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>该命令会将 Fortify 的行为类发布到你的 <code v-pre>app/Actions</code> 目录，如果该目录不存在，则会创建该目录。此外，还将发布 Fortify 的配置（<code v-pre>FortifyServiceProvider</code>）和迁移文件。</p>
<p>下一步，你应该迁移数据库：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="fortify-服务提供商" tabindex="-1"><a class="header-anchor" href="#fortify-服务提供商"><span>Fortify 服务提供商</span></a></h3>
<p>上面讨论的 <code v-pre>vendor:publish</code> 命令还将发布 <code v-pre>App\Providers\FortifyServiceProvider</code> 类。你应该确保该类已在应用程序的 <code v-pre>config/app.php</code> 配置文件的 <code v-pre>providers</code> 数组中注册。</p>
<p>Fortify 服务提供商注册了 Fortify 所发布的行为类，并指导 Fortify 在执行各自的任务时使用它们。</p>
<h3 id="fortify-包含的功能" tabindex="-1"><a class="header-anchor" href="#fortify-包含的功能"><span>Fortify 包含的功能</span></a></h3>
<p>该 <code v-pre>fortify</code> 配置文件包含一个 <code v-pre>features</code> 配置数组。该数组默路定义了 Fortify 的路由和功能。如果你不打算将 Fortify 与 <a href="https://jetstream.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Jetstream</a> 配合使用，我们建议你仅启用以下功能，这是大多数 Laravel 应用提供的基本身份认证功能：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'features'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">registration</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">resetPasswords</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">emailVerification</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="禁用视图" tabindex="-1"><a class="header-anchor" href="#禁用视图"><span>禁用视图</span></a></h3>
<p>默认情况下，Fortify 定义用于返回视图的路由，例如登录或注册。但是，如果要构建 JavaScript 驱动的单页应用，那么可能不需要这些路由。因此，你可以通过将 <code v-pre>config/fortify.php</code> 配置文件中的 <code v-pre>views</code> 配置值设为 <code v-pre>false</code> 来禁用这些路由：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'views'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="禁用视图-重置密码" tabindex="-1"><a class="header-anchor" href="#禁用视图-重置密码"><span>禁用视图 &amp; 重置密码</span></a></h4>
<p>如果你选择禁用 Fortify 的视图，并且将为你的应用实现重置密码功能，这时你仍然需要定义一个名为 <code v-pre>password.reset</code> 的路由，该路由负责显示应用的「重置密码」视图。这是必要的，因为 Laravel 的 <code v-pre>Illuminate\Auth\Notifications\ResetPassword</code> 通知将通过名为 <code v-pre>password.reset</code> 的路由生成重置密码 URL。</p>
<h2 id="身份认证" tabindex="-1"><a class="header-anchor" href="#身份认证"><span>身份认证</span></a></h2>
<p>首先，我们需要指导 Fortify 如何返回「登录」视图。记住，Fortify 是一个无头认证扩展。如果你想要一个已经为你完成的 Laravel 身份认证功能的前端实现，你应该使用 <a href="https://learnku.com/docs/laravel/9.x/starter-kits" target="_blank" rel="noopener noreferrer">应用入门套件</a>。</p>
<p>所有的身份认证视图逻辑，都可以使用 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的方法来自定义。通常，你应该从应用的 <code v-pre>App\Providers\FortifyServiceProvider</code> 的 <code v-pre>boot</code> 方法中调用此方法。Fortify 将负责定义返回此视图的 <code v-pre>/login</code> 路由：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">loginView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你的登录模板应包括一个向 <code v-pre>/login</code> 发出 POST 请求的表单。 <code v-pre>/login</code> 表单需要一个 <code v-pre>email</code> / <code v-pre>username</code> 和 <code v-pre>password</code>。 <code v-pre>email</code> / <code v-pre>username</code> 字段与 <code v-pre>config/fortify.php</code> 配置文件中的 <code v-pre>username</code> 值相匹配。另外，可以提供布尔值 <code v-pre>remember</code> 字段来指导用户想要使用 Laravel 提供的「记住我」功能。</p>
<p>如果登录尝试成功，Fortify 会将你重定向到通过应用程序 <code v-pre>fortify</code> 配置文件中的 <code v-pre>home</code> 配置选项配置的 URI。如果登录请求是 XHR 请求，将返回 200 HTTP 响应。</p>
<p>如果请求不成功，用户将被重定向回登录页，验证错误将通过共享的 <code v-pre>$errors</code> <a href="https://learnku.com/docs/laravel/10.x/validation#quick-displaying-the-validation-errors" target="_blank" rel="noopener noreferrer">Blade 模板变量</a> 提供给你。或者，在 XHR 请求的情况下，验证错误将与 422 HTTP 响应一起返回。</p>
<h3 id="自定义用户认证" tabindex="-1"><a class="header-anchor" href="#自定义用户认证"><span>自定义用户认证</span></a></h3>
<p>Fortify 将根据提供的凭据和为你的应用程序配置的身份验证保护自动检索和验证用户。但是，你有时可能希望对登录凭据的身份验证和用户的检索方式进行完全自定义。幸运的是，Fortify 允许你使用 <code v-pre>Fortify::authenticateUsing</code> 方法轻松完成此操作。</p>
<p>此方法接受接收传入 HTTP 请求的闭包。闭包负责验证附加到请求的登录凭据并返回关联的用户实例。如果凭据无效或找不到用户，则闭包应返回 <code v-pre>null</code> 或 <code v-pre>false</code> 。通常，这个方法应该从你的 <code v-pre>FortifyServiceProvider</code> 的 <code v-pre>boot</code> 方法中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Hash</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导应用服务</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">authenticateUsing</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$user</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">            <span class="token class-name static-context">Hash</span><span class="token operator">::</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token property">password</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">password</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="身份验证看守器" tabindex="-1"><a class="header-anchor" href="#身份验证看守器"><span>身份验证看守器</span></a></h4>
<p>你可以在应用程序的 <code v-pre>fortify</code> 文件中自定义 Fortify 使用的身份验证看守器。但是，你应该确保配置的看守器是 <code v-pre>Illuminate\Contracts\Auth\StatefulGuard</code> 的实现。如果你尝试使用 Laravel Fortify 对 SPA 进行身份验证，你应该将 Laravel 的默认 <code v-pre>web</code> 防护与 <a href="https://learnku.com/docs/laravel/sanctum" target="_blank" rel="noopener noreferrer">Laravel Sanctum</a> 结合使用。</p>
<h3 id="自定义身份验证管道" tabindex="-1"><a class="header-anchor" href="#自定义身份验证管道"><span>自定义身份验证管道</span></a></h3>
<p>Laravel Fortify 通过可调用类的管道对登录请求进行身份验证。如果你愿意，你可以定义一个自定义的类管道，登录请求应该通过管道传输。每个类都应该有一个 <code v-pre>__invoke</code> 方法，该方法接收传入 <code v-pre>Illuminate\Http\Request</code> 实例的方法，并且像 <a href="https://learnku.com/docs/laravel/10.x/middleware" target="_blank" rel="noopener noreferrer">中间件</a> 一样，调用一个 <code v-pre>$next</code> 变量，以便将请求传递给管道中的下一个类。</p>
<p>要定义自定义管道，可以使用 <code v-pre>Fortify::authenticateThrough</code> 方法。此方法接受一个闭包，该闭包应返回类数组，以通过管道传递登录请求。通常，应该从 <code v-pre>App\Providers\FortifyServiceProvider</code> 的 <code v-pre>boot</code> 方法调用此方法。</p>
<p>下面的示例包含默认管道定义，你可以在自己进行修改时将其用作开始：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Actions<span class="token punctuation">\</span>AttemptToAuthenticate</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Actions<span class="token punctuation">\</span>EnsureLoginIsNotThrottled</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Actions<span class="token punctuation">\</span>PrepareAuthenticatedSession</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Actions<span class="token punctuation">\</span>RedirectIfTwoFactorAuthenticatable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">authenticateThrough</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">array_filter</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fortify.limiters.login'</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token constant">null</span> <span class="token punctuation">:</span> <span class="token class-name static-context">EnsureLoginIsNotThrottled</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">enabled</span><span class="token punctuation">(</span><span class="token class-name static-context">Features</span><span class="token operator">::</span><span class="token function">twoFactorAuthentication</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token class-name static-context">RedirectIfTwoFactorAuthenticatable</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token punctuation">:</span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">AttemptToAuthenticate</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">PrepareAuthenticatedSession</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义跳转" tabindex="-1"><a class="header-anchor" href="#自定义跳转"><span>自定义跳转</span></a></h3>
<p>如果登录尝试成功，Fortify 会将你重定向到你应用程序 <code v-pre>Fortify</code> 的配置文件中的 <code v-pre>home</code> 配置选项的 URI 值。如果登录请求是 XHR 请求，将返回 200 HTTP 响应。用户注销应用程序后，该用户将被重定向到 <code v-pre>/</code> 地址。</p>
<p>如果需要对这种行为进行高级定制，可以将 <code v-pre>LoginResponse</code> 和 <code v-pre>LogoutResponse</code> 契约的实现绑定到 Laravel <a href="https://learnku.com/docs/laravel/10.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 。通常，这应该在你应用程序的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>register</code> 方法中完成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>LogoutResponse</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>RedirectResponse</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 注册任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">instance</span><span class="token punctuation">(</span><span class="token class-name static-context">LogoutResponse</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token keyword">class</span> <span class="token class-name-definition class-name">implements</span> <span class="token class-name">LogoutResponse</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toResponse</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="双因素认证" tabindex="-1"><a class="header-anchor" href="#双因素认证"><span>双因素认证</span></a></h2>
<p>当 Fortify 的双因素身份验证功能启用时，用户需要在身份验证过程中输入一个六位数的数字令牌。该令牌使用基于时间的一次性密码（TOTP）生成，该密码可以从任何与 TOTP 兼容的移动认证应用程序（如 Google Authenticator）中检索。</p>
<p>在开始之前，你应该首先确保应用程序的 <code v-pre>App\Models\User</code> 模型使用 <code v-pre>Laravel\Fortify\TwoFactorAuthenticatable</code> trait：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>TwoFactorAuthenticatable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">,</span> TwoFactorAuthenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，你应该在应用程序中构建一个页面，用户可以在其中管理他们的双因素身份验证设置。该页面应允许用户启用和禁用双因素身份验证，以及重新生成双因素身份验证恢复的代码。</p>
<blockquote>
<p>默认情况下， <code v-pre>fortify</code> 配置文件的 <code v-pre>features</code> 数组管理着 Fortify 的双因素身份验证设置在修改前需要密码确认。因此，在使用之前，你的应用程序应该实现 Fortify 的 <a href="#password-confirmation">密码确认</a> 功能。</p>
</blockquote>
<h3 id="启用双因素身份验证" tabindex="-1"><a class="header-anchor" href="#启用双因素身份验证"><span>启用双因素身份验证</span></a></h3>
<p>要启用双重身份验证，你的应用程序应向 Fortify 定义的 <code v-pre>/user/two-factor-authentication</code> 发出 POST 请求。如果请求成功，用户将被重定向回之前的 URL，并且 <code v-pre>status</code> session 变量将设置为 <code v-pre>two-factor-authentication-enabled</code>。你可以在模板中检测这个 <code v-pre>status</code> session 变量以显示适当的成功消息。如果请求是 XHR 请求，将返回 <code v-pre>200</code> HTTP 响应：</p>
<p>在选择启用双因素认证后，用户仍然必须通过提供一个有效的双因素认证代码来「确认」他们的双因素认证配置。因此，你的「成功」消息应该指示用户，双因素认证的确认仍然是必需的。</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line">@if (session('status') == 'two-factor-authentication-enabled')</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mb-4 font-medium text-sm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">        Please finish configuring two factor authentication below.</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，你应该显示双重身份验证二维码，供用户扫描到他们的身份验证器应用程序中。如果你使用 Blade 呈现应用程序的前端，则可以使用用户实例上可用的 <code v-pre>twoFactorQrCodeSvg</code> 方法检索二维码 SVG：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">twoFactorQrCodeSvg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你正在构建由 JavaScript 驱动的前端，你可以向 <code v-pre>/user/two-factor-qr-code</code> 发出 XHR GET 请求以检索用户的双重身份验证二维码。将返回一个包含 <code v-pre>svg</code> 键的 JSON 对象。</p>
<h4 id="确认双因素认证" tabindex="-1"><a class="header-anchor" href="#确认双因素认证"><span>确认双因素认证</span></a></h4>
<p>除了显示用户的双因素认证 QR 码，你应该提供一个文本输入，用户可以提供一个有效的认证码来「确认」他们的双因素认证配置。这个代码应该通过 POST 请求提供到 <code v-pre>/user/confirmed-two-factor-authentication</code>，由 Fortify 来进行确认。</p>
<p>If the request is successful, the user will be redirected back to the previous URL and the <code v-pre>status</code> session variable will be set to <code v-pre>two-factor-authentication-confirmed</code>:</p>
<p>如果请求成功，用户将被重定向到之前的URL，<code v-pre>status</code> 会话变量将被设置为 `two-factor-authentication-confirmed'。</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line">@if (session('status') == 'two-factor-authentication-confirmed')</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mb-4 font-medium text-sm<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">        Two factor authentication confirmed and enabled successfully.</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果对双因素认证确认端点的请求是通过 XHR 请求进行的，将返回一个 <code v-pre>200</code> HTTP响应。</p>
<h4 id="显示恢复代码" tabindex="-1"><a class="header-anchor" href="#显示恢复代码"><span>显示恢复代码</span></a></h4>
<p>你还应该显示用户的两个因素恢复代码。这些恢复代码允许用户在无法访问其移动设备时进行身份验证。如果你使用 Blade 来渲染应用程序的前端，你可以通过经过身份验证的用户实例访问恢复代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">(</span><span class="token keyword type-casting">array</span><span class="token punctuation">)</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">recoveryCodes</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你正在构建一个 JavaScript 驱动的前端，你可以向 <code v-pre>/user/two-factor-recovery-codes</code> 端点发出 XHR GET 请求。此端点将返回一个包含用户恢复代码的 JSON 数组。</p>
<p>要重新生成用户的恢复代码，你的应用程序应向 <code v-pre>/user/two-factor-recovery-codes</code> 端点发出 POST 请求。</p>
<h3 id="使用双因素身份验证进行身份验证" tabindex="-1"><a class="header-anchor" href="#使用双因素身份验证进行身份验证"><span>使用双因素身份验证进行身份验证</span></a></h3>
<p>在身份验证过程中，Fortify 将自动将用户重定向到你的应用程序的双因素身份验证检查页面。但是，如果你的应用程序正在发出 XHR 登录请求，则在成功进行身份验证尝试后返回的 JSON 响应将包含一个具有 <code v-pre>two_factor</code> 布尔属性的 JSON 对象。你应该检查此值以了解是否应该重定向到应用程序的双因素身份验证检查页面。</p>
<p>要开始实现两因素身份验证功能，我们需要指示 Fortify 如何返回我们的双因素身份验证检查页面。Fortify 的所有身份验证视图渲染逻辑都可以使用通过 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常，你应该从应用程序的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">twoFactorChallengeView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.two-factor-challenge'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify 将负责定义返回此视图的 <code v-pre>/two-factor-challenge</code> 路由。你的 <code v-pre>two-factor-challenge</code> 模板应包含一个向 <code v-pre>/two-factor-challenge</code> 端点发出 POST 请求的表单。 <code v-pre>/two-factor-challenge</code> 操作需要包含有效 TOTP 令牌的 <code v-pre>code</code> 字段或包含用户恢复代码之一的 <code v-pre>recovery_code</code> 字段。</p>
<p>如果登录尝试成功，Fortify 会将用户重定向到通过应用程序的 <code v-pre>fortify</code> 配置文件中的 <code v-pre>home</code> 配置选项配置的 URI。如果登录请求是 XHR 请求，将返回 204 HTTP 响应。</p>
<p>如果请求不成功，用户将被重定向回两因素挑战屏幕，验证错误将通过共享的 <code v-pre>$errors</code> <a href="https://learnku.com/docs/laravel/10.x/%E9%AA%8C%E8%AF%81#%E5%BF%AB%E9%80%9F%E6%98%BE%E7%A4%BA%E9%AA%8C%E8%AF%81%E9%94%99%E8%AF%AF" target="_blank" rel="noopener noreferrer">Blade 模板变量</a>。或者，在 XHR 请求的情况下，验证错误将返回 422 HTTP 响应。</p>
<h3 id="禁用两因素身份验证" tabindex="-1"><a class="header-anchor" href="#禁用两因素身份验证"><span>禁用两因素身份验证</span></a></h3>
<p>要禁用双因素身份验证，你的应用程序应向 <code v-pre>/user/two-factor-authentication</code> 端点发出 DELETE 请求。请记住，Fortify 的两个因素身份验证端点在被调用之前需要 <a href="#password-confirmation">密码确认</a>。</p>
<h2 id="注册" tabindex="-1"><a class="header-anchor" href="#注册"><span>注册</span></a></h2>
<p>要开始实现我们应用程序的注册功能，我们需要指示 Fortify 如何返回我们的“注册”视图。请记住，Fortify 是一个无头身份验证库。如果你想要一个已经为你完成的 Laravel 身份验证功能的前端实现，你应该使用 <a href="https://learnku.com/docs/laravel/10.x/starter-kits" target="_blank" rel="noopener noreferrer">application starter kit</a>。</p>
<p>Fortify 的所有视图渲染逻辑都可以使用通过 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常，你应该从 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">registerView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.register'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify 将负责定义返回此视图的 <code v-pre>/register</code> 路由。你的 <code v-pre>register</code> 模板应包含一个向 Fortify 定义的 <code v-pre>/register</code> 端点发出 POST 请求的表单。</p>
<p><code v-pre>/register</code> 端点需要一个字符串 <code v-pre>name</code>、字符串电子邮件地址/用户名、<code v-pre>password</code> 和 <code v-pre>password_confirmation</code> 字段。电子邮件/用户名字段的名称应与应用程序的 <code v-pre>fortify</code> 配置文件中定义的 <code v-pre>username</code> 配置值匹配。</p>
<p>如果注册尝试成功，Fortify 会将用户重定向到通过应用程序的 <code v-pre>fortify</code> 配置文件中的 <code v-pre>home</code> 配置选项配置的 URI。如果登录请求是 XHR 请求，将返回 201 HTTP 响应。</p>
<p>如果请求不成功，用户将被重定向回注册屏幕，验证错误将通过共享的 <code v-pre>$errors</code> <a href="https://learnku.com/docs/laravel/10.x/validation#%E5%BF%AB%E9%80%9F%E6%98%BE%E7%A4%BA%E9%AA%8C%E8%AF%81%E9%94%99%E8%AF%AF" target="_blank" rel="noopener noreferrer">Blade 模板变量</a>。或者，在 XHR 请求的情况下，验证错误将返回 422 HTTP 响应。</p>
<h3 id="定制注册" tabindex="-1"><a class="header-anchor" href="#定制注册"><span>定制注册</span></a></h3>
<p>可以通过修改安装 Laravel Fortify 时生成的 <code v-pre>App\Actions\Fortify\CreateNewUser</code> 操作来自定义用户验证和创建过程。</p>
<h2 id="重设密码" tabindex="-1"><a class="header-anchor" href="#重设密码"><span>重设密码</span></a></h2>
<h3 id="请求密码重置链接" tabindex="-1"><a class="header-anchor" href="#请求密码重置链接"><span>请求密码重置链接</span></a></h3>
<p>要开始实现我们应用程序的密码重置功能，我们需要指示 Fortify 如何返回我们的“忘记密码”视图。请记住，Fortify 是一个无头身份验证库。如果你想要一个已经为你完成的 Laravel 身份验证功能的前端实现，你应该使用 <a href="https://learnku.com/docs/laravel/10.x/starter-kits" target="_blank" rel="noopener noreferrer">application starter kit</a>。</p>
<p>Fortify 的所有视图渲染逻辑都可以使用通过 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常，你应该从应用程序的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">requestPasswordResetLinkView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.forgot-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify 将负责定义返回此视图的 <code v-pre>/forgot-password</code> 端点。你的 <code v-pre>forgot-password</code> 模板应该包含一个向 <code v-pre>/forgot-password</code> 端点发出 POST 请求的表单。</p>
<p><code v-pre>/forgot-password</code> 端点需要一个字符串 <code v-pre>email</code> 字段。此字段/数据库列的名称应与应用程序的 <code v-pre>fortify</code> 配置文件中的 <code v-pre>email</code> 配置值匹配。</p>
<h4 id="处理密码重置链接请求响应" tabindex="-1"><a class="header-anchor" href="#处理密码重置链接请求响应"><span>处理密码重置链接请求响应</span></a></h4>
<p>如果密码重置链接请求成功，Fortify 会将用户重定向回 <code v-pre>/forgot-password</code> 端点，并向用户发送一封电子邮件，其中包含可用于重置密码的安全链接。如果请求是 XHR 请求，将返回 200 HTTP 响应。</p>
<p>在请求成功后被重定向到 <code v-pre>/forgot-password</code> 端点，<code v-pre>status</code> 会话变量可用于显示密码重置链接请求的状态。</p>
<p>在成功请求后被重定向回 <code v-pre>/forgot-password</code> 端点后，<code v-pre>status</code> 会话变量可用于显示密码重置链接请求尝试的状态。此会话变量的值将匹配应用程序的 <code v-pre>password</code> <a href="https://learnku.com/docs/laravel/10.x/localization" target="_blank" rel="noopener noreferrer">语言文件</a> 中定义的翻译字符串之一：</p>
<div class="language-html line-numbers-mode" data-highlighter="prismjs" data-ext="html" data-title="html"><pre v-pre class="language-html"><code><span class="line">@if (session('status'))</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>mb-4 font-medium text-sm text-green-600<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
<span class="line">        {{ session('status') }}</span>
<span class="line">    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span></span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果请求不成功，用户将被重定向回请求密码重置链接屏幕，验证错误将通过共享的 <code v-pre>$errors</code> <a href="https://learnku.com/docs/laravel/10.x/validation#quick-displaying-the-validation-errors" target="_blank" rel="noopener noreferrer">Blade 模板变量</a> 提供给你。或者，在 XHR 请求的情况下，验证错误将返回 422 HTTP 响应。</p>
<h3 id="重设密码-1" tabindex="-1"><a class="header-anchor" href="#重设密码-1"><span>重设密码</span></a></h3>
<p>为了完成应用程序的密码重置功能，我们需要指示 Fortify 如何返回我们的「重置密码」视图。</p>
<p>Fortify 的所有视图渲染逻辑都可以使用通过 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常，你应该从应用程序的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">resetPasswordView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.reset-password'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'request'</span> <span class="token operator">=></span> <span class="token variable">$request</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify 将负责定义显示此视图的路线。你的 <code v-pre>reset-password</code> 模板应该包含一个向 <code v-pre>/reset-password</code> 发出 POST 请求的表单。</p>
<p><code v-pre>/reset-password</code> 端点需要一个字符串 <code v-pre>email</code> 字段、一个 <code v-pre>password</code> 字段、一个 <code v-pre>password_confirmation</code> 字段和一个名为 <code v-pre>token</code> 的隐藏字段，其中包含<code v-pre>request()-&gt;route('token')</code>。 <code v-pre>email</code> 字段/数据库列的名称应与应用程序的 <code v-pre>fortify</code> 配置文件中定义的 <code v-pre>email</code> 配置值匹配。</p>
<h4 id="处理密码重置响应" tabindex="-1"><a class="header-anchor" href="#处理密码重置响应"><span>处理密码重置响应</span></a></h4>
<p>如果密码重置请求成功，Fortify 将重定向回 <code v-pre>/login</code> 路由，以便用户可以使用新密码登录。此外，还将设置一个 <code v-pre>status</code> 会话变量，以便你可以在登录屏幕上显示重置的成功状态：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@if (session('status'))</span>
<span class="line">    &lt;div class=&quot;mb-4 font-medium text-sm text-green-600&quot;&gt;</span>
<span class="line">        {{ session('status') }}</span>
<span class="line">    &lt;/div&gt;</span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果请求是 XHR 请求，将返回 200 HTTP 响应。</p>
<p>如果请求不成功，用户将被重定向回重置密码屏幕，验证错误将通过共享的 <code v-pre>$errors</code> <a href="https://learnku.com/docs/laravel/10.x/validation#%E5%BF%AB%E9%80%9F%E6%98%BE%E7%A4%BA%E9%AA%8C%E8%AF%81%E9%94%99%E8%AF%AF" target="_blank" rel="noopener noreferrer">Blade 模板变量</a>。或者，在 XHR 请求的情况下，验证错误将返回 422 HTTP 响应。</p>
<h3 id="自定义密码重置" tabindex="-1"><a class="header-anchor" href="#自定义密码重置"><span>自定义密码重置</span></a></h3>
<p>可以通过修改安装 Laravel Fortify 时生成的 <code v-pre>App\Actions\ResetUserPassword</code> 操作来自定义密码重置过程。</p>
<h2 id="电子邮件验证" tabindex="-1"><a class="header-anchor" href="#电子邮件验证"><span>电子邮件验证</span></a></h2>
<p>注册后，你可能希望用户在继续访问你的应用程序之前验证他们的电子邮件地址。要开始使用，请确保在 <code v-pre>fortify</code> 配置文件的 <code v-pre>features</code> 数组中启用了 <code v-pre>emailVerification</code> 功能。接下来，你应该确保你的 <code v-pre>App\Models\User</code> 类实现了 <code v-pre>Illuminate\Contracts\Auth\MustVerifyEmail</code> 接口。</p>
<p>完成这两个设置步骤后，新注册的用户将收到一封电子邮件，提示他们验证其电子邮件地址的所有权。但是，我们需要通知 Fortify 如何显示电子邮件验证屏幕，通知用户他们需要点击电子邮件中的验证链接。</p>
<p>Fortify 的所有视图的渲染逻辑都可以使用通过 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常，你应该从应用程序的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导所有应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">verifyEmailView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.verify-email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当用户被 Laravel 内置的 <code v-pre>verified</code> 中间件重定向到 <code v-pre>/email/verify</code> 端点时，Fortify 将负责定义显示此视图的路由。</p>
<p>你的 <code v-pre>verify-email</code> 模板应包含一条信息性消息，指示用户单击发送到其电子邮件地址的电子邮件验证链接。</p>
<h4 id="重新发送电子邮件验证链接" tabindex="-1"><a class="header-anchor" href="#重新发送电子邮件验证链接"><span>重新发送电子邮件验证链接</span></a></h4>
<p>如果你愿意，你可以在应用程序的 <code v-pre>verify-email</code> 模板中添加一个按钮，该按钮会触发对 <code v-pre>/email/verification-notification</code> 端点的 POST 请求。当此端点收到请求时，将通过电子邮件将新的验证电子邮件链接发送给用户，如果先前的验证链接被意外删除或丢失，则允许用户获取新的验证链接。</p>
<p>如果重新发送验证链接电子邮件的请求成功，Fortify 将使用 <code v-pre>status</code> 会话变量将用户重定向回 <code v-pre>/email/verify</code> 端点，允许你向用户显示信息性消息，通知他们操作已完成成功的。如果请求是 XHR 请求，将返回 202 HTTP 响应：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@if (session('status') == 'verification-link-sent')</span>
<span class="line">    &lt;div class=&quot;mb-4 font-medium text-sm text-green-600&quot;&gt;</span>
<span class="line">        A new email verification link has been emailed to you!</span>
<span class="line">    &lt;/div&gt;</span>
<span class="line">@endif</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="保护路由" tabindex="-1"><a class="header-anchor" href="#保护路由"><span>保护路由</span></a></h3>
<p>要指定一个路由或一组路由要求用户验证他们的电子邮件地址，你应该将 Laravel 的内置 <code v-pre>verified</code> 中间件附加到该路由。该中间件在你的应用程序的 <code v-pre>App\Http\Kernel</code> 类中注册：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">middleware</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'verified'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="确认密码" tabindex="-1"><a class="header-anchor" href="#确认密码"><span>确认密码</span></a></h2>
<p>在构建应用程序时，你可能偶尔会有一些操作需要用户在执行操作之前确认其密码。通常，这些路由受到 Laravel 内置的 <code v-pre>password.confirm</code> 中间件的保护。</p>
<p>要开始实现密码确认功能，我们需要指示 Fortify 如何返回应用程序的「密码确认」视图。请记住，Fortify 是一个无头身份验证库。如果你想要一个已经为你完成的 Laravel 身份验证功能的前端实现，你应该使用 <a href="https://learnku.com/docs/laravel/10.x/starter-kits" target="_blank" rel="noopener noreferrer">application starter kit</a>。</p>
<p>Fortify 的所有视图渲染逻辑都可以使用通过 <code v-pre>Laravel\Fortify\Fortify</code> 类提供的适当方法进行自定义。通常，你应该从应用程序的 <code v-pre>App\Providers\FortifyServiceProvider</code> 类的 <code v-pre>boot</code> 方法调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Fortify<span class="token punctuation">\</span>Fortify</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导所有应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Fortify</span><span class="token operator">::</span><span class="token function">confirmPasswordView</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.confirm-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Fortify 将负责定义返回此视图的 <code v-pre>/user/confirm-password</code> 端点。你的 <code v-pre>confirm-password</code> 模板应包含一个表单，该表单向 <code v-pre>/user/confirm-password</code> 端点发出 POST 请求。 <code v-pre>/user/confirm-password</code> 端点需要一个包含用户当前密码的 <code v-pre>password</code> 字段。</p>
<p>如果密码与用户的当前密码匹配，Fortify 会将用户重定向到他们尝试访问的路由。如果请求是 XHR 请求，将返回 201 HTTP 响应。</p>
<p>如果请求不成功，用户将被重定向回确认密码屏幕，验证错误将通过共享的 <code v-pre>$errors</code> Blade 模板变量提供给你。或者，在 XHR 请求的情况下，验证错误将返回 422 HTTP 响应。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/fortify/14904" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/fo...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/fortify/14904" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/fo...</a></p>
</blockquote>
</div></template>


